import { _decorator, Component, Label, Node, Sprite } from 'cc';
import { Tech } from '../entity/Tech';
import { UIManager } from '../common/UIManager';
import { PrefabEnum } from '../common/PrefabEnum';
import { Events } from '../entity/Events';
import { Utils } from '../common/Utils';
import { UserInfo } from '../common/UserInfo';
import { EventOper } from '../entity/Buildings';
import VillageApi from '../api/VillageApi';
const { ccclass, property } = _decorator;

@ccclass('Developing')
export class Developing extends Component {
    @property(Node)
    bg: Node
    @property(Label)
    title: Label
    @property(Node)
    close: Node
    @property(Node)
    develop: Node
    @property(Sprite)
    cover: Sprite
    @property(Sprite)
    border: Sprite
    @property(Label)
    soldierName: Label
    @property(Label)
    time: Label

    offTime: number
    techData: Tech = null

    start() {
        this.close.on(Node.EventType.TOUCH_END, this.onClose, this)
        this.bg.on(Node.EventType.TOUCH_END, this.onClose, this)
        this.develop.on(Node.EventType.TOUCH_END, this.onSpeed, this)
    }

    async setData(tech: Tech) {
        this.techData = tech;
        this.title.string = tech.name + '研究'
        this.cover.spriteFrame = await tech.getTechImg()
        this.soldierName.string = tech.name

        let event = this.techData.getStudyEvent()
        if (event) {
            this.countDown(event)
        }
    }

    countDown(event: Events) {
        this.unschedule(this.updateTime)
        if (!event) {
            return
        }

        let eStartTime = event.eStartTime
        this.offTime = eStartTime - Math.floor(UserInfo.instance.serverTime / 1000)
        if (this.offTime <= 0) {
            this.onClose()
            return
        }

        this.time.string = `${Utils.secondToStr(this.offTime)}`
        this.schedule(this.updateTime, 1, this.offTime - 1)
    }

    updateTime() {
        this.offTime--
        this.time.string = `${Utils.secondToStr(this.offTime)}`

        if (this.offTime <= 0) {
            // 倒计时完成
            // 清除事件，刷新页面
            // this.soldierData.developComplete()
            UserInfo.instance.refreshVillageDetail()
            this.onClose()
        }
    }

    onClose() {
        UIManager.instance.closeUI(PrefabEnum.Developling)
    }

    async onSpeed() {
        let event = this.techData.getStudyEvent()
        if (!event) {
            this.onClose()
            return
        }

        let param = {
            ac: EventOper.SPEED_BUILD,
            eid: event.eHandle
        }

        await UIManager.instance.showLoading()
        let res = await VillageApi.eventOper(param)
        UIManager.instance.closeLoading()
        console.log(`加速升级，ac =${param.ac} ,del =${param.eid} `, res)

        res = await UserInfo.instance.refreshVillageDetail()
       
        this.onClose()

        // event = this.techData.getStudyEvent()
        // if (event) {
        //     let eStartTime = event.eStartTime
        //     this.offTime = eStartTime - Math.floor(UserInfo.instance.serverTime / 1000)
        //     if (this.offTime <= 0) {
        //         this.onClose()
        //         return;
        //     }

        //     this.time.string = `${Utils.secondToStr(this.offTime)}`
        //     this.schedule(this.updateTime, 1, this.offTime - 1)
        // } else {
        //     this.onClose()
        //     return;
        // }

    }
}


